<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="试卷名" prop="safetyExaminationPaperName">
				<el-input v-model="queryParams.safetyExaminationPaperName" placeholder="请输入试卷名" clearable size="small"
				 @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<div v-if="router == 1">
				<el-col :span="1.5">
					<el-col :span="1.5" v-if="paperuser != 1">
					<el-button type="warning" plain icon="el-icon-check" size="mini" :disabled="multiple" @click="confirmuse">确认使用</el-button>
					</el-col>
					<el-button type="info" plain icon="el-icon-check" size="mini" @click="usedpaper">已使用的试卷</el-button>
					<el-button type="primary" plain icon="el-icon-check" size="mini" @click="unusedpaper">未使用的试卷</el-button>
				</el-col>
			</div>
			<div v-else>
				<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:testpaperinformation:add']">添加试卷</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:testpaperinformation:remove']">删除试卷</el-button>
			</el-col>
			</div>
			
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="examinationpaperinfoList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="试卷号" align="center" prop="safetyExaminationPaperId" />
			<el-table-column label="试卷名" align="center" prop="safetyExaminationPaperName" />
			<el-table-column label="试卷题数" align="center" prop="safetyExaminationQuestionAmount" />
			<el-table-column label="试卷使用次数" align="center" prop="safetyExaminationUse" />
			<el-table-column label="合格题数" align="center" prop="qualifiedAmount" />
			<el-table-column label="设置人员" align="center" prop="setId" />
			<el-table-column label="设置时间" align="center" prop="setDate" />
			<el-table-column label="查看详情" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:testpaperinformation:edit']">查看详情</el-button>
				</template>
			</el-table-column>

			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<router-link :to="{path:'/safetyexam/examquestionbank',query:{ safetyExaminationPaperId: scope.row.safetyExaminationPaperId,memo:2,examinationId:$route.query.examinationId }}">
						<el-button type="text" icon="el-icon-edit-outline" size="mini" v-hasPermi="['system:testpaperinformation:viewtopic']">查看题目</el-button>
					</router-link>
					<div v-if="router ==0">
						<router-link :to="{path:'/safetyexam/examquestionbank',query:{ safetyExaminationPaperId: scope.row.safetyExaminationPaperId,memo:1}}">
							<el-button type="text" icon="el-icon-edit-outline" size="mini" v-hasPermi="['system:testpaperinformation:addtopic']">新增题目</el-button>
						</router-link>
					</div>
				</template>
			</el-table-column>


		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 修改试卷信息对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="试卷名" prop="safetyExaminationPaperName">
					<el-input v-model="form.safetyExaminationPaperName" placeholder="请输入试卷名" />
				</el-form-item>
				<el-form-item label="试卷题数" prop="safetyExaminationQuestionAmount">
					<el-input v-model="form.safetyExaminationQuestionAmount" :readonly="true" placeholder="请输入试卷题数" />
				</el-form-item>
				<el-form-item label="试卷使用次数" prop="safetyExaminationUse">
					<el-input v-model="form.safetyExaminationUse" :readonly="true" placeholder="请输入试卷使用次数" />
				</el-form-item>
				<el-form-item label="合格题数" prop="qualifiedAmount">
					<el-input v-model="form.qualifiedAmount" placeholder="请输入合格题数" />
				</el-form-item>
				<el-form-item label="设置人员" prop="setId">
					<el-input v-model="form.setId" :readonly="true" placeholder="请输入设置人员" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
		<!-- 添加试卷信息对话框 -->
		<el-dialog :title="titlepaper" :visible.sync="paperopen" width="500px" append-to-body>
			<el-form ref="paperform" :model="paperform" :rules="paperrules" label-width="80px">
				<el-form-item label="试卷名" prop="safetyExaminationPaperName">
					<el-input v-model="paperform.safetyExaminationPaperName" placeholder="请输入试卷名" />
				</el-form-item>
				<el-form-item label="合格题数" prop="qualifiedAmount">
					<el-input v-model="paperform.qualifiedAmount" placeholder="请输入合格题数" />
				</el-form-item>
				<el-form-item label="设置人员" prop="setId">
					<el-input v-model="paperform.setId" :readonly="true" placeholder="请输入设置人员" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="confirmpaper">确定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listExaminationpaperinfo,
		getExaminationpaperinfo,
		delExaminationpaperinfo,
		addExaminationpaperinfo,
		updateExaminationpaperinfo,
		exportExaminationpaperinfo,
		insertExaminationPapers
	} from "@/api/system/examinationpaperinfo";
	import store from '@/store'
	export default {
		name: "Examinationpaperinfo",
		components: {},
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				//判断是否传值跳转页面
				router: 0,
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				//调配处理标题
				titlepaper: "",
				paperopen: false,
				paperform: {},
				// 试卷信息表格数据
				paperuser: 0,
				examinationpaperinfoList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					safetyExaminationPaperName: null,
					safetyExaminationQuestionAmount: null,
					safetyExaminationUse: null,
					qualifiedAmount: null,
					setId: null,
					setDate: null
				},
				//确认使用试卷参数
				papersParams: {
					paper: [],
					safetyExaminationId: null,
					setId: null
				},
				safetyExaminationPaperId: null,
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					safetyExaminationPaperName: [{
						required: true,
						message: "试卷名不能为空",
						trigger: "blur"
					}],
					safetyExaminationQuestionAmount: [{
						required: true,
						message: "试卷题数不能为空",
						trigger: "blur"
					}],
					safetyExaminationUse: [{
						required: true,
						message: "试卷使用次数不能为空",
						trigger: "blur"
					}],
					qualifiedAmount: [{
						required: true,
						message: "合格题数不能为空",
						trigger: "blur"
					}],
					setId: [{
						required: true,
						message: "设置人员不能为空",
						trigger: "blur"
					}],
					setDate: [{
						required: true,
						message: "设置时间不能为空",
						trigger: "blur"
					}]
				},
				paperrules: {
					safetyExaminationPaperName: [{
						required: true,
						message: "试卷名不能为空",
						trigger: "blur"
					}],
					qualifiedAmount: [{
						required: true,
						message: "合格题数不能为空",
						trigger: "blur"
					}],
					setId: [{
						required: true,
						message: "设置人员不能为空",
						trigger: "blur"
					}]
				}
			};
		},
		created() {
			this.getList();
			if (this.$route.query.examinationId != null) {
				this.router = 1;
			}
		},
		methods: {
			/** 查询试卷信息列表 */
			getList() {
				this.loading = true;
				if(this.$route.query.examinationId != null){
					this.queryParams.safetyExaminationId=this.$route.query.examinationId;
					this.queryParams.Type = 2;
				}
				listExaminationpaperinfo(this.queryParams).then(response => {
					this.examinationpaperinfoList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.paperopen = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					safetyExaminationPaperId: null,
					safetyExaminationPaperName: null,
					safetyExaminationQuestionAmount: null,
					safetyExaminationUse: null,
					qualifiedAmount: null,
					setId: null,
					setDate: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.safetyExaminationPaperId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.paperopen = true;
				this.titlepaper = "添加试卷信息";
				this.paperform.setId = store.getters.userId;
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const safetyExaminationPaperId = row.safetyExaminationPaperId || this.ids
				getExaminationpaperinfo(safetyExaminationPaperId).then(response => {
					this.form = response.data;
					this.form.setId = store.getters.userId;
					this.open = true;
					this.title = "修改试卷信息";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.safetyExaminationPaperId != null) {
							updateExaminationpaperinfo(this.form).then(response => {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			//添加试卷确认按钮
			confirmpaper() {
				this.$refs["paperform"].validate(valid => {
					if (valid) {
						if (this.paperform.safetyExaminationPaperName != null) {
							addExaminationpaperinfo(this.paperform).then(response => {
								this.msgSuccess("添加成功");
								this.paperopen = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const safetyExaminationPaperIds = row.safetyExaminationPaperId || this.ids;
				this.$confirm('是否确认删除试卷编号为"' + safetyExaminationPaperIds + '"的试卷?', "警告", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning"
				}).then(function() {
					return delExaminationpaperinfo(safetyExaminationPaperIds);
				}).then(() => {
					this.getList();
					this.msgSuccess("删除成功");
				})
			},
			//确认使用试卷按钮操作
			confirmuse(row) {
				var that = this;
				that.papersParams.paper = [];
				const safetyExaminationPaperIds = row.safetyExaminationPaperId || this.ids;
				this.$confirm('是否确认使用试卷编号为"' + safetyExaminationPaperIds + '"的试卷?', "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "success"
				}).then(function() {
					for (let val of safetyExaminationPaperIds) {
						that.papersParams.paper.push({
							'safetyExaminationPaperId': val
						});
					}
					that.papersParams.safetyExaminationId = that.$route.query.examinationId;
					that.papersParams.setId = store.getters.userId;
					return insertExaminationPapers(that.papersParams);
				}).then(() => {
					this.getList();
					this.msgSuccess("使用成功");
					this.$router.push({
						path: "/safetyexam/safetyexam"
					});
				})
			},
			//查看已使用试卷按钮
			usedpaper(){
				this.loading = true;
				this.queryParams.safetyExaminationId=this.$route.query.examinationId;
				this.queryParams.Type = 1;
				listExaminationpaperinfo(this.queryParams).then(response => {
					this.examinationpaperinfoList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
				this.paperuser=1;
			},
			//未使用试卷的按钮
			unusedpaper(){
				this.loading = true;
				this.queryParams.safetyExaminationId=this.$route.query.examinationId;
				this.queryParams.Type = 2;
				listExaminationpaperinfo(this.queryParams).then(response => {
					this.examinationpaperinfoList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
				this.paperuser=2;
			}
		}
	};
</script>
